using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    /// <summary>
    /// 激活存档点
    /// 参数组1: 1：存档点tag(0表示自己)，2：是否激活（1：激活，0：禁用）
    /// </summary>
    public class ExecActiveCheckPoint : BaseServiceExecution
    {
        public override void Execute(LevelRegion region, LevelPlayer player)
        {
            var config = _config as ExecParamActiveCheckPoint;
            if (!config.tagOrMcid.TryGetValue(triggerEntity, out int tagOrMcid))
            {
                LogicLog.LogError("无法获取到tagOrMcid.");
                return;
            }
            bool isToActive = config.isToActive;
            LevelEntity checkPointEntity = tagOrMcid == 0 ? triggerEntity : region.GetEntityByTagOrMcid(tagOrMcid);
            if (checkPointEntity == null)
            {
                LogicLog.LogError($"找不到实体，tagOrMcid：{tagOrMcid}");
                return;
            }
            if (!checkPointEntity.HasDynamicParam(EntityDynamicParamType.CheckPoint))
            {
                LogicLog.LogError($"指定实体不是存档点，tagOrMcid：{tagOrMcid}");
                return;
            }
            EntityPropCheckPoint checkPointProp = checkPointEntity.GetDynamicProperty(EntityDynamicPropertyType.CheckPoint) as EntityPropCheckPoint;
            if (checkPointProp.isActive == isToActive)
            {
                return;
            }
            checkPointProp.isActive = isToActive;
            region.NotifyActiveCheckPoint(player, checkPointEntity, checkPointProp.isActive);
        }
    }
}
